package com.iris.live.services.report.sources;

import com.iris.live.services.common.NumberUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.ReportRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/12/6.
 */
@Component("ExportBoutiqueContract")
public class ExportBoutiqueContract implements IExcelReportSource {


    @Autowired
    private ReportRepository repository;
    /**
     * 获取Sheet定义
     * @param context
     * @return
     */
    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商 经销商_精品明细导出模板", "经销商 经销商_精品明细导出模板", true);
        return sheets;
    }

    /**
     * 获取当前Sheet需要的数据集
     * @param context
     * @param sheet
     * @return
     * @throws ParseException
     */
    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        DecimalFormat df = new DecimalFormat("######0.00");
        String dealer = (String) context.get("dealer");
        Map params = (Map)context.get("param");
        String[] columns = (String[]) context.get("columns"), columnPair;
        //keys存放数据Key， titles存放列表头
        String[] keys = new String[columns.length], titles = new String[columns.length + 1];

        int index;
        List<Row> rows = new ArrayList<>();
        for(index = 0; index < 2; index++) {
            rows.add(new Row());
        }




//        RequestBase request = (RequestBase) context.get("request");
        String quoId = params.get("quoId").toString();
        int total = 0;
        //应收金额
        Double money = 0d;
        //精品总金额
        Double zongJi = 0d;
        //总计减免
        Double jianMian = 0d;

        String heTongId = null,sc = null,cust= null,carModel= null,lianxiren= null,diZhi= null,phone= null,waiGuan= null,vin= null,fadongId = null;


        List datas = repository.getBoutique(dealer,quoId);
        for (Object obj : datas) {
            Map map = (Map)obj;
            heTongId = null == map.get("合同号") ? null : map.get("合同号").toString();
            sc = null == map.get("销售顾问") ? null : map.get("销售顾问").toString();
            cust = null == map.get("客户名称") ? null : map.get("客户名称").toString();
            carModel = null == map.get("车型") ? null : map.get("车型").toString();
            lianxiren = null == map.get("联系人") ? null : map.get("联系人").toString();
            diZhi = null == map.get("联系地址") ? null : map.get("联系地址").toString();
            phone = null == map.get("联系电话") ? null : map.get("联系电话").toString();
            waiGuan = null == map.get("外观") ? null : map.get("外观").toString();
            vin = null == map.get("车架号") ? null : map.get("车架号").toString();
            fadongId = null == map.get("发动机号") ? null : map.get("发动机号").toString();
        }

        Map cc = new HashMap();
        cc.put("合同号",heTongId);
        cc.put("销售顾问",sc);
        cc.put("客户名称",cust);
        cc.put("车型",carModel);
        cc.put("联系人",lianxiren);
        cc.put("联系地址",diZhi);
        cc.put("联系电话",phone);
        cc.put("外观",waiGuan);
        cc.put("车架号",vin);
        cc.put("发动机号",fadongId);


        cc.put("钥匙号",null);
        cc.put("车牌号",null);
        cc.put("客户签名",null);
        cc.put("审核人",null);
        cc.put("审核时间",null);
        cc.put("备注",null);

        //3行
        rows.add(new Row(setShuJu1(cc)));
        rows.add(new Row(setShuJu2(cc)));
        rows.add(new Row(setShuJu3(cc)));
        rows.add(new Row());

        //标题输出
        titles[0] = "NO.";
        for(index = 0; index < columns.length; index++){
            columnPair = columns[index].split(",");
            keys[index] = columnPair[1];
            titles[index+1] = columnPair[0];
        }
        rows.add(new Row(getTitleCells(titles)));

        //数据输出
        index = 0;
        if (datas != null) {
            for (Object o : datas) {
                Map mmm = (Map)o;

                //第7行
                rows.add(new Row(getDataCells(index++, keys, o)));
                total +=Integer.parseInt(mmm.get("数量").toString());
                money +=Double.parseDouble(mmm.get("金额").toString());
                zongJi +=Double.parseDouble(mmm.get("精品总金额").toString());

            }

                rows.add(new Row());

            jianMian= zongJi - money;
            cc.put("精品总金额",df.format(zongJi));
            cc.put("应收总金额",df.format(money));
            cc.put("总计减免",df.format(jianMian));
            rows.add(new Row(setShuJu4(cc)));
            rows.add(new Row(setShuJu5(cc)));
            rows.add(new Row(setShuJu6(cc)));
        }


        return rows;
    }

    /**
     * 获取Excel 标题行
     * @param titles
     * @return
     */
    private  Cell[] getTitleCells(String[] titles){
        Cell[] cells = new Cell[titles.length];
        for(int index = 0; index < titles.length; index++){
            cells[index] = new Cell(titles[index], index );
        }

        return cells;
    }

    /**
     * 获取数据行
     * @param rowIndex
     * @param columns
     * @param o
     * @return
     */
    private Cell[] getDataCells(int rowIndex, String[] columns, Object o) {
        Map map = (Map) o;

        Cell[] cells = new Cell[columns.length + 1];
        cells[0] = new Cell(rowIndex +1 + "", 0);

        Object value = null;
        for(int index = 0; index < columns.length; index++){
            value = map.get(columns[index]);
            //获取数据类型，用以作为写入excel时的数据类型（修改原因：数据写入excel时，生产号以科学计数法的形式显示）
            String type = null;
            if (!StringUtils.isNullOrBlank(map.get(columns[index]))) {
                type = map.get(columns[index]).getClass().getSimpleName();
            }

            if(StringUtils.isNullOrBlank(value)){
                cells[index + 1] = new Cell("", index + 1);
            }else if(NumberUtils.isDouble(map.get(columns[index])) && ("Double".equals(type) || "Decimal".equals(type)) || "BigDecimal".equals(type)){ //Double型小数点后保留两位, Added by Tony, 2017-06-27
                cells[index + 1] = new Cell(NumberUtils.format(Double.valueOf(map.get(columns[index]).toString()), 2), index + 1);
            }else {
                cells[index + 1] = new Cell(map.get(columns[index]) == null ? "" : map.get(columns[index]).toString(), index + 1);
            }
        }

        return cells;
    }

    private Cell[] setShuJu1(Map map) {

        Cell[] cells = new Cell[11];
        int index = 0;
        cells[index++] = new Cell("合同号：", index);
        cells[index++] = new Cell(map.get("合同号") == null ? "" : map.get("合同号").toString(), index);
        cells[index++] = new Cell("销售顾问：", index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell(map.get("销售顾问") == null ? "" : map.get("销售顾问").toString(), index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell("客户名称：", index);
        cells[index++] = new Cell(map.get("客户名称") == null ? "" : map.get("客户名称").toString(), index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell("车型", index);
        cells[index++] = new Cell(map.get("车型") == null ? "" : map.get("车型").toString(), index);

        return cells;
    }
    private Cell[] setShuJu2(Map map) {

        Cell[] cells = new Cell[11];
        int index = 0;
        cells[index++] = new Cell("联系人：", index);
        cells[index++] = new Cell(map.get("联系人") == null ? "" : map.get("联系人").toString(), index);
        cells[index++] = new Cell("联系地址：", index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell(map.get("联系地址") == null ? "" : map.get("联系地址").toString(), index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell("联系电话：", index);
        cells[index++] = new Cell(map.get("联系电话") == null ? "" : map.get("联系电话").toString(), index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell("外观：", index);
        cells[index++] = new Cell(map.get("外观") == null ? "" : map.get("外观").toString(), index);

        return cells;
    }
    private Cell[] setShuJu3(Map map) {

        Cell[] cells = new Cell[11];
        int index = 0;
        cells[index++] = new Cell("车架号：", index);
        cells[index++] = new Cell(map.get("车架号") == null ? "" : map.get("车架号").toString(), index);
        cells[index++] = new Cell("发动机号：", index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell(map.get("发动机号") == null ? "" : map.get("发动机号").toString(), index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell("钥匙号：", index);
        cells[index++] = new Cell(map.get("钥匙号") == null ? "" : map.get("钥匙号").toString(), index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell("车牌号：", index);
        cells[index++] = new Cell(map.get("车牌号") == null ? "" : map.get("车牌号").toString(), index);

        return cells;
    }

    private Cell[] setShuJu4(Map map) {

        Cell[] cells = new Cell[6];
        int index = 0;
        cells[index++] = new Cell("精品总金额：", index);
        cells[index++] = new Cell(map.get("精品总金额") == null ? "" : map.get("精品总金额").toString(), index);
        cells[index++] = new Cell("应收总金额：", index);
//        cells[index++] = new Cell("", index);
//        cells[index++] = new Cell("", index);
        cells[index++] = new Cell(map.get("应收总金额") == null ? "" : map.get("应收总金额").toString(), index);
//        cells[index++] = new Cell("", index);
        cells[index++] = new Cell("总计减免：", index);
//        cells[index++] = new Cell("", index);
        cells[index++] = new Cell(map.get("总计减免") == null ? "" : map.get("总计减免").toString(), index);

        return cells;
    }

    private Cell[] setShuJu5(Map map) {

        Cell[] cells = new Cell[6];
        int index = 0;
        cells[index++] = new Cell("客户签名：", index);
        cells[index++] = new Cell(map.get("客户签名") == null ? "" : map.get("客户签名").toString(), index);
        cells[index++] = new Cell("审核人：", index);
//        cells[index++] = new Cell("", index);
//        cells[index++] = new Cell("", index);
        cells[index++] = new Cell(map.get("审核人") == null ? "" : map.get("审核人").toString(), index);
//        cells[index++] = new Cell("", index);
        cells[index++] = new Cell("审核时间：", index);
//        cells[index++] = new Cell("", index);
        cells[index++] = new Cell(map.get("审核时间") == null ? "" : map.get("审核时间").toString(), index);

        return cells;
    }

    private Cell[] setShuJu6(Map map) {

        Cell[] cells = new Cell[3];
        int index = 0;
        cells[index++] = new Cell("备注：", index);
        cells[index++] = new Cell("", index);
        cells[index++] = new Cell(map.get("备注") == null ? "" : map.get("备注").toString(), index);

        return cells;
    }

}
